<template>
	<view class="index1_content">
		<!-- 导航栏 -->
		<view class="search-header">
			<view @click="handeleTo">
				<image style="width: 15rpx;height: 30rpx;margin-bottom: -15rpx;" src="@/static/index2/index7.png"
					mode=""></image>
			</view>
			<view style="color: #FFFFFF;font-size: 32rpx;margin-right: 10rpx;margin-bottom: -15rpx;">{{$t('费用维护')}}
			</view>
			<view></view>
		</view>
		<view class="edit-block">
			<view class="edit-block1" style="text-align:right">
				<view style="height: 60rpx;display: flex;align-items: center;">
					<image style="width: 11rpx;height: 11rpx;margin-bottom: 15rpx;" src="@/static/index2/index11.png"
						mode=""></image>
					<view style="margin-left: 10rpx;">{{$t('出发城市')}}</view>
				</view>
				
				<view>
					<formCity style="width: 100%;" :texthidden="true" :is_register="1" :item="cityItemState"
						:is_port="true" @change="citySelectCbStart" />
				</view>

			</view>
			<view class="edit-block1" style="text-align:right">
				<view style="height: 60rpx;display: flex;align-items: center;">
					<image style="width: 11rpx;height: 11rpx;margin-bottom: 15rpx;" src="@/static/index2/index11.png"
						mode=""></image>
					<view style="margin-left: 10rpx;">{{$t('到达城市')}}</view>
				</view>
				
				<view>
					<formCity style="width: 100%;" :texthidden="true" :is_register="1" :item="cityItemState"
						:is_port="true" @change="citySelectCbEnd" />
				</view>
			</view>
			<view class="edit-block2">
				<view style="height: 60rpx;display: flex;align-items: center;">
					<image style="width: 11rpx;height: 11rpx;margin-bottom: 15rpx;" src="@/static/index2/index11.png"
						mode=""></image>
					<view style="margin-left: 10rpx;">{{$t('到货时间(天)')}}</view>
				</view>
				<view class="edit-block4" style="margin-top: 10rpx;">
					<view class="edit-block3">
						<u-input type="number" placeholder-style="font-size: 26rpx;" :placeholder="$t('请输入到货天数')"
							input-align="center" v-model="model.startDay"></u-input>
					</view>
					<view class="edit-one">
						<view class="edit-one1"></view>
					</view>
					<view class="edit-block3">
						<u-input type="number" placeholder-style="font-size: 26rpx;" :placeholder="$t('请输入到货天数')"
							input-align="center" v-model="model.endDay"></u-input>
					</view>
				</view>
			</view>
			<view class="edit-block1">
				<view style="height: 60rpx;display: flex;align-items: center;">
					<image style="width: 11rpx;height: 11rpx;margin-bottom: 15rpx;" src="@/static/index2/index11.png"
						mode=""></image>
					<view style="margin-left: 10rpx;">{{$t('备注')}}</view>
				</view>
				<u-input placeholder-style="font-size: 26rpx;" :placeholder="$t('请输入备注信息')" input-align="right"
					v-model="model.remarks"></u-input>
			</view>
			<view class="edit-ladder">
				<view class="edit-ladder1">
					<view>{{$t('阶梯费用(可添加多个)')}}</view>
					<image @click="handeleAdd" style="width: 30rpx;height: 30rpx;" src="@/static/index2/index13.png"
						mode=""></image>
				</view>

				<view class="edit-ladder3" v-for="(item,index) in model.section" :key="index">
					<view class="edit-ladder2">

						<view style="color: #1D1D1D;font-size: 24rpx;"> {{$t('密度区间')}} kg(m³)：</view>
						<view
							style="width: 100%;display: flex;align-items: center;justify-content: space-between;margin-top: 16rpx;">
							<view class="edit-ladder2-sec">
								<u-input type="number" input-align="center" v-model="model.section[index].sectionStart"
									placeholder-style="font-size: 24rpx;" :placeholder="$t('请输入区间')"></u-input>
							</view>
							<view class="edit-ladder2-ru">
								<view class="edit-ladder2-ru1"></view>
							</view>
							<view class="edit-ladder2-sec">
								<u-input type="number" input-align="center" v-model="model.section[index].sectionEnd"
									placeholder-style="font-size: 24rpx;" :placeholder="$t('请输入区间')"></u-input>
							</view>
						</view>
						<view
							style="display: flex;align-items: center;justify-content: space-between;margin-top: 24rpx;width: 100%;">
							<view style="color: #1D1D1D;font-size: 24rpx;">{{$t('价格(元/kg)')}}：</view>
							<view class="edit-ladder-p">
								<u-input type="number" input-align="center" v-model="model.section[index].sectionPrice"
									placeholder-style="font-size: 24rpx;" :placeholder="$t('请输入价格')"></u-input>
							</view>
						</view>
					</view>
					<image v-if="model.section.length>1" @click="handeleRemove(index)"
						style="width: 30rpx;height: 30rpx;" src="@/static/index2/index12.png" mode=""></image>
				</view>

			</view>
		</view>

		<view class="edit-submit" @click="sub()">
			{{requesting?$t("正在提交"):$t('保存')}}
		</view>

		<!-- 顶部背景色 -->
		<image style="width: 750rpx;height: 690rpx;position: absolute;top: 0;z-index: 1;"
			src="@/static/index2/20241101130857.png" mode=""></image>
	</view>
</template>

<script>
	import CitySelect from './components/citySelect.vue'
	import formCity from './components/formCitys.vue'
	export default {
		components: {
			CitySelect,
			formCity
		},
		data() {
			return {
				requesting: false,
				lang: getApp().globalData.lang,
				textStart: this.$t('请选择出发城市'),
				textEnd: this.$t('请选择到达城市'),
				model: {
					departureCity: '', //出发城市
					arrivingCity: '', //到达城市
					startDay: '', //天数
					endDay: '', //天数
					remarks: '', //备注
					section: [{
						sectionStart: '', //区间开始
						sectionEnd: '', //区间结束
						sectionPrice: '' //区间价格
					}],
				},
				cityItemState: {
					key: "city_id",
					label: false,
					placeholder: this.$t('所在城市'),
					type: "city",
					required: true,
					value: [],
					rules: {},
				},
			}
		},
		onLoad(options) {
			if (options.item) {
				var json = JSON.parse(options.item)
				if (this.lang == 'zh') {
					this.textStart = json.departure.mergename
					this.textEnd = json.reach.mergename
					this.model.remarks = json.remark
				} else {
					this.textStart = json.departure.mergename_ru
					this.textEnd = json.reach.mergename_ru
					this.model.remarks = json.remark_ru
				}

				this.model.departureCity = json.departure_country
				this.model.arrivingCity = json.reach_country
				this.model.startDay = json.send_start_day
				this.model.endDay = json.send_end_day

				this.model.id = json.id || null

				var arr = []
				for (var i = 0; i < json.cost_list.length; i++) {
					arr.push({
						sectionStart: json.cost_list[i]['min_density'],
						sectionEnd: json.cost_list[i]['max_density'],
						sectionPrice: json.cost_list[i]['price']
					})
				}

				console.log(arr)
				this.model.section = arr


			}
		},
		methods: {
			// 删除
			handeleRemove(index) {
				uni.showModal({
					title: this.$t('确认删除'),
					content: this.$t('是否确认删除当前文件'),
					confirmText: this.$t('确认'),
					cancelText: this.$t('取消'),
					success: res => {
						if (res.confirm) {
							this.model.section.splice(index, 1)

						} else if (res.cancel) {}
					}
				});
			},
			handeleAdd() {
				this.model.section.push({
					sectionStart: '', //区间开始
					sectionEnd: '', //区间结束
					sectionPrice: '' //区间价格
				})
			},
			// 出发城市
			citySelectCbStart(e, d) {
				this.departureCity = d[2].value
				this.model.departureCity = d[2].value
				this.textStart = [d[0].label, d[1].label, d[2].label].join(",")
			},
			// 到达城市
			citySelectCbEnd(e, d) {
				this.arrivingCity = d[2].value
				this.model.arrivingCity = d[2].value
				this.textEnd = [d[0].label, d[1].label, d[2].label].join(",")
			},
			handeleTo() {
				uni.navigateBack()
			},
			sub() {
				if (this.requesting) return
				if (Number(this.model.startDay) > Number(this.model.endDay)) {
					this.toast(this.$t("到货时间:起始区间不能大于结束区间"))
					return
				}

				if (!this.model.section[0]) {
					this.toast(this.$t("至少设置一个阶梯费用"))
					return
				}
				for (let item of this.model.section) {
					if (Number(item.sectionStart) <= 0 || Number(item.sectionPrice) <= 0 || Number(item.sectionEnd) <=
						0) {
						this.toast(this.$t("请填写正确的密度区间与价格"))
						return
					} else {
						if (Number(item.sectionStart) > Number(item.sectionEnd)) {
							this.toast(this.$t("密度:起始区间不能大于结束区间"))
							return
						}
					}
				}

				var that = this

				if (this.model.section.length > 20) {
					this.toast(this.$t("最多可设置20个阶梯费用"))
					return
				}

				let str = this.model.section.map(item =>
					`${item.sectionStart},${item.sectionEnd},${item.sectionPrice}`).join(";")


				if (!this.model.departureCity) {
					this.toast(this.$t("请选择") + this.$t("出发城市"))
					return
				}

				if (!this.model.arrivingCity) {
					this.toast(this.$t("请选择") + this.$t("到达城市"))
					return
				}

				if (!this.model.remarks) {
					this.toast(this.$t("请输入") + this.$t("备注"))
					return
				}

				if (!this.model.startDay) {
					this.toast(this.$t("请输入") + this.$t("到货天数"))
					return
				}

				if (!this.model.endDay) {
					this.toast(this.$t("请输入") + this.$t("到货天数"))
					return
				}

				var data = {
					id: this.model.id,
					remark: this.model.remarks,
					send_start_day: this.model.startDay,
					send_end_day: this.model.endDay,
					departure_country: this.model.departureCity,
					reach_country: this.model.arrivingCity,
					list: str
				}

				uni.showLoading({
					title: this.$t("正在提交")
				})
				this.requesting = true
				that.$u.api.transport.cost_save(data).then(res => {
					uni.hideLoading()
					uni.showModal({
						content: that.$t('操作成功'),
						showCancel: false,
						confirmText: this.$t('是'),
						success: e => {
							if (e.confirm) {
								uni.navigateBack()
							}
						}
					})
					setTimeout(() => {
						this.requesting = false
					}, 1000)
				})


			}
		}
	}
</script>

<style scoped>
	.edit-submit {
		width: 540rpx;
		height: 90rpx;
		background: #5282E6;
		color: #FFFFFF;
		font-size: 28rpx;
		border-radius: 90rpx;
		text-align: center;
		line-height: 90rpx;
		margin-top: 40rpx;
	}

	.edit-ladder2-ru1 {
		width: 20rpx;
		height: 4rpx;
		background: #999999;
	}

	.edit-ladder2-ru {
		width: 40rpx;
		height: 40rpx;
		background: #F6F7FB;
		border-radius: 40rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.edit-ladder2-sec {
		width: 187rpx;
		height: 40rpx;
		background: #F6F7FB;
		border-radius: 40rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.edit-ladder-p {
		width: 284rpx;
		height: 40rpx;
		background: #F6F7FB;
		border-radius: 40rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.edit-ladder3 {
		width: 100%;
		min-height: 204rpx;
		display: flex;
		justify-content: space-between;
		align-items: flex-end;
		margin-bottom: 20rpx;
	}

	.edit-ladder2 {
		width: 530rpx;
		min-height: 204rpx;
		border-radius: 17rpx;
		padding: 30rpx 30rpx 38rpx 30rpx;
		box-sizing: border-box;
		background-color: #FFFFFF;
	}

	.edit-ladder1 {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 26rpx;
		margin-bottom: 18rpx;
	}

	.edit-ladder {
		width: 630rpx;
		min-height: 100rpx;
		background: #F6F7FB;
		border-radius: 20rpx;
		margin-top: 24rpx;
		padding: 30rpx 30rpx 10rpx 30rpx;
		box-sizing: border-box;
	}

	.edit-one1 {

		width: 20rpx;
		height: 4rpx;
		background: #1D1D1D;
	}

	.edit-one {
		width: 62rpx;
		height: 62rpx;
		border-radius: 62rpx;
		background: #F6F7FB;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.edit-block4 {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.edit-block3 {
		width: 254rpx;
		height: 62rpx;
		border-radius: 62rpx;
		background: #F6F7FB;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.edit-block2 {
		width: 100%;
		height: 162rpx;
		display: flex;
		flex-direction: column;
		border-bottom: 1rpx solid #D8D8D8;
	}

	.edit-block1 {
		width: 100%;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-bottom: 1rpx solid #D8D8D8;
	}

	.edit-block {
		z-index: 2;
		width: 690rpx;
		min-height: 528rpx;
		border-radius: 32rpx;
		background: #FFFFFF;
		padding: 30rpx;
		box-sizing: border-box;
		margin-top: 30rpx;
	}

	.search-header {
		z-index: 2;
		width: 690rpx;
		height: 103rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 40rpx;
		margin-bottom: -20rpx;
	}

	.index1_content {
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;

	}

	page {
		width: 100%;
		height: 100%;
		background-color: #F1F2F4;
	}
</style>